using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;
namespace CMS.BLL
{
    public class StaticPages
    {
        #region Common
        public eStaticPages Data;

        public StaticPages()
        {
            Data = new eStaticPages();
        }

        ~StaticPages()
        {
        }

        public virtual void Dispose()
        {
        }

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cStaticPages objDAL = new cStaticPages();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cStaticPages objDAL = new cStaticPages();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cStaticPages objInfor = new cStaticPages();
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.StaticPageID ;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
            cStaticPages objInfor = new cStaticPages();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public int UpdateStatus(int ID, int Status)
        {
            int nResult = 0;
            cStaticPages objDAL = new cStaticPages();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.Status = Status;
              //  objDAL.DateCreated  = DateTime.Today;
                objDAL.Save();
                nResult = objDAL.StaticPageID ;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        private void MappingData(cStaticPages objInfor)
        {

            objInfor.PageCode = Data.PageCode;
            objInfor.PageName1 = Data.PageName1;
            objInfor.PageName2 = Data.PageName2;
            objInfor.PageName3 = Data.PageName3;
            objInfor.MenuName1 = Data.MenuName1;
            objInfor.MenuName2 = Data.MenuName2;
            objInfor.MenuName3 = Data.MenuName3;
            objInfor.Parent = Data.Parent;
            objInfor.PageContent1 = Data.PageContent1;
            objInfor.PageContent2 = Data.PageContent2;
            objInfor.PageContent3 = Data.PageContent3;
            objInfor.DateCreated = Data.DateCreated;
            objInfor.Images = Data.Images;
            objInfor.Priority = Data.Priority;
            objInfor.Status = Data.Status;
            objInfor.Type = Data.Type;
            objInfor.PageLink = Data.PageLink;
            objInfor.InitContent1 = Data.InitContent1;
            objInfor.InitContent2 = Data.InitContent2;
            objInfor.InitContent3 = Data.InitContent3;
        }

        private void ConvertData(cStaticPages objInfor)
        {
            Data.PageCode=objInfor.PageCode  ;
            Data.PageName1  =objInfor.PageName1 ;
            Data.PageName2 =objInfor.PageName2  ;
            Data.PageName3=objInfor.PageName3  ;
            Data.MenuName1 =objInfor.MenuName1  ;
            Data.MenuName2 =objInfor.MenuName2  ;
            Data.MenuName3 =objInfor.MenuName3  ;
            Data.Parent=objInfor.Parent ;
            Data.PageContent1 =objInfor.PageContent1 ;
            Data.PageContent2   =objInfor.PageContent2  ;
            Data.PageContent3  =objInfor.PageContent3 ;
            Data.DateCreated =objInfor.DateCreated  ;
            Data.Images=objInfor.Images  ;
            Data.Priority =objInfor.Priority ;
            Data.Status=objInfor.Status ;
            Data.Type=objInfor.Type  ;
            Data.PageLink = objInfor.PageLink;
            Data.InitContent1 = objInfor.InitContent1;
            Data.InitContent2 = objInfor.InitContent2;
            Data.InitContent3 = objInfor.InitContent3;
        }

        public DataTable Search(string Title, int Parent, int DefaultLangId, int CurrentLangId, int Status, int PageIndex, int PageSize, ref int PageCount)
        {
            DataTable tbResult = new DataTable();
            cStaticPages objDAL = new cStaticPages();
            tbResult = objDAL.Search( Title,  Parent,  DefaultLangId,  CurrentLangId,  Status,  PageIndex,  PageSize, ref  PageCount);
            return tbResult;
        }

        public DataTable Seach2(string Title, int Parent, int DefaultLangId, int CurrentLangId, int Status, int PageIndex, int PageSize, ref int PageCount)
        {
            DataTable tbResult = new DataTable();
            cStaticPages objDAL = new cStaticPages();
            tbResult = objDAL.Search2(Title, Parent, DefaultLangId, CurrentLangId, Status, PageIndex, PageSize, ref  PageCount);
            return tbResult;
        }

        public DataTable GetItemByPageCode(string PageCode)
        {
            DataTable tbResult = new DataTable();
            cStaticPages objDAL = new cStaticPages();
            tbResult = objDAL.GetItemByPageCode(PageCode);
            return tbResult;
        }
        public int GetIDbyPageCode(string PageCode)
        {
            int nResult = 0;
            cStaticPages objDAL = new cStaticPages();
            nResult = objDAL.GetIDbyPageCode(PageCode);
            return nResult;
        }

        public void DeleteChildItem(int ParentPage)
        {
            cStaticPages objDAL = new cStaticPages();
            objDAL.DeleteChildItem(ParentPage);
        }

        public bool CheckExistPage(string PageCode)
        {
            bool isExist = false;
            cStaticPages objDAL = new cStaticPages();
            isExist = objDAL.CheckExistPage(PageCode);
            return isExist;
        }
        #endregion
    }
}
